package com.dl698.axdr;

import com.dl698.asn1.axdr.AxdrType;
import com.dl698.util.ReverseByteArrayInputStream;
import com.dl698.util.ReverseByteArrayOutputStream;

/**
 * DateTime类
 * 实现日期时间的编解码
 * 
 * @author wind
 * @version 1.1.1
 */
public class DateTime implements AxdrType {

    private byte[] dataCode = null;
    private Unsigned16 year = null;
    private Unsigned8 month = null;
    private Unsigned8 dayOfMonth = null;
    private Unsigned8 dayOfWeek = null;
    private Unsigned8 hour = null;
    private Unsigned8 minute = null;
    private Unsigned8 second = null;
    private Unsigned16 milliseconds = null;

    /**
     * 默认构造函数
     */
    public DateTime() {
    }

    /**
     * 设置数据编码
     * 
     * @param dataCode 数据编码
     */
    public void setDataCode(byte[] dataCode) {
        this.dataCode = dataCode;
    }

    /**
     * 设置日期时间参数
     * 
     * @param year         年
     * @param month        月
     * @param dayOfMonth   日
     * @param dayOfWeek    星期
     * @param hour         小时
     * @param minute       分钟
     * @param second       秒
     * @param milliseconds 毫秒
     */
    public void setDateTime(Unsigned16 year, Unsigned8 month, Unsigned8 dayOfMonth,
            Unsigned8 dayOfWeek, Unsigned8 hour, Unsigned8 minute,
            Unsigned8 second, Unsigned16 milliseconds) {
        this.year = year;
        this.month = month;
        this.dayOfMonth = dayOfMonth;
        this.dayOfWeek = dayOfWeek;
        this.hour = hour;
        this.minute = minute;
        this.second = second;
        this.milliseconds = milliseconds;
    }

    @Override
    public int decode(ReverseByteArrayInputStream input) {
        int codeLength = 0;

        year = new Unsigned16();
        year.setConst();
        codeLength += year.decode(input);

        month = new Unsigned8();
        month.setConst();
        codeLength += month.decode(input);

        dayOfMonth = new Unsigned8();
        dayOfMonth.setConst();
        codeLength += dayOfMonth.decode(input);

        dayOfWeek = new Unsigned8();
        dayOfWeek.setConst();
        codeLength += dayOfWeek.decode(input);

        hour = new Unsigned8();
        hour.setConst();
        codeLength += hour.decode(input);

        minute = new Unsigned8();
        minute.setConst();
        codeLength += minute.decode(input);

        second = new Unsigned8();
        second.setConst();
        codeLength += second.decode(input);

        milliseconds = new Unsigned16();
        milliseconds.setConst();
        codeLength += milliseconds.decode(input);

        return codeLength;
    }

    @Override
    public int encode(ReverseByteArrayOutputStream output) {
        int codeLength;

        if (dataCode != null) {
            codeLength = dataCode.length;
            for (int i = dataCode.length - 1; i >= 0; i--) {
                try {
                    output.write(dataCode[i]);
                } catch (java.io.IOException e) {
                    throw new RuntimeException("IO exception during encode", e);
                }
            }
        } else {
            if (year != null && month != null && dayOfWeek != null
                    && dayOfMonth != null && hour != null && minute != null
                    && second != null) {
                codeLength = 0;
                codeLength += milliseconds.encode(output);
                codeLength += second.encode(output);
                codeLength += minute.encode(output);
                codeLength += hour.encode(output);
                codeLength += dayOfWeek.encode(output);
                codeLength += dayOfMonth.encode(output);
                codeLength += month.encode(output);
                codeLength += year.encode(output);
            } else {
                throw new RuntimeException("date_time is null");
            }
        }
        return codeLength;
    }

    /**
     * 编码并保存
     * 
     * @param encodingSizeGuess 编码大小预估
     */
    public void encodeAndSave(int encodingSizeGuess) {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        encode(revOStream);
        this.dataCode = revOStream.getArray();
    }

    @Override
    public String toString() {
        if (year != null && month != null && dayOfMonth != null
                && dayOfWeek != null && hour != null && minute != null
                && second != null) {
            return "sequence: {" + "year: " + year + ", month: " + month + ", day_of_month: " + dayOfMonth
                    + ", day_of_week" + dayOfWeek + ", hour: " + hour + ", minute: " + minute + ", second: " + second
                    + "}";
        } else {
            return "date_time is null";
        }
    }
}